package com.itbour.web.action.yjx.trade;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.yjx.trade.CdInfoDto;
import com.itbour.dto.yjx.trade.YjxFormInfo;
import com.itbour.dto.yjx.trade.YjxFormMgrDto;
import com.itbour.service.yjx.trade.YjxFormMgrService;
import com.opensymphony.xwork2.ActionContext;

/** 	@author 李琦 kris
 * */
public class GetFormMgrAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private YjxFormMgrService yjxFormMgrService;
	
	
	private String findFormType;
	
	private Map<String, Object> resMap =new HashMap<String, Object>();
	public String getFormMgrInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findFormType)){
				param.put("form_type",findFormType);
			}
			Page page = yjxFormMgrService.getFormMgrInfo(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}
	
	private String findUniDocId;
	private String findUserId;
	private String findtype;
	public String getFormInfoMgr(){// 获取用户表单反馈列表信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			resMap.clear();
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findUniDocId)){
				param.put("uni_doc_id",findUniDocId);
			}
			if (StringUtils.isNotEmpty(findUserId)){
				param.put("user_id",findUserId);
			}
			if (StringUtils.isNotEmpty(findtype)){
				param.put("info_type",findtype);
			}
			Page page = yjxFormMgrService.pageGetFormInfoMgr(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
		}		
		return SUCCESS;
	}
	public String getFindUniDocId() {
		return findUniDocId;
	}

	public void setFindUniDocId(String findUniDocId) {
		this.findUniDocId = findUniDocId;
	}

	public String getFindUserId() {
		return findUserId;
	}
	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}
	public String getFindtype() {
		return findtype;
	}
	public void setFindtype(String findtype) {
		this.findtype = findtype;
	}
	
	
	public List<YjxFormInfo> formInfoLis; // 获取单个 表单反馈信息
	public String getOneFormInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String infoId = request.getParameter("infoId");
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("info_id",infoId);
			formInfoLis = yjxFormMgrService.getOneFormInfo(parm);
    	}		
		return SUCCESS;
	}
	
	

	private List<YjxFormMgrDto> formLis;
	public String getOneFormMgrInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String formId = request.getParameter("formId");
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("form_id",formId);
			formLis = yjxFormMgrService.getOneFormMgr(parm);
    	}		
		return SUCCESS;
	}

	private YjxFormMgrDto formMgrDto;
	public void editFormMgrInfo() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
    		formMgrDto.setUpdate_by(user.getUser_id().toString());
    		boolean b = yjxFormMgrService.editFormMgrInfo(formMgrDto);
    		if (b){
    			this.writeJSON("ok"); 
    		} else {
    			this.writeJSON("no"); 
    		}
    	} else {
    		this.writeJSON("no");
    	}
	}
	public YjxFormMgrDto getFormMgrDto() {
		return formMgrDto;
	}
	public void setFormMgrDto(YjxFormMgrDto formMgrDto) {
		this.formMgrDto = formMgrDto;
	}
	
	

	public void delFormMgr() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if (user!=null && user.getUser_id()!=null && ids!=null){
			Map<String, Object> parm =new HashMap<String, Object>();
			String[] st = StringUtils.str2Array(ids);
			parm.put("ids", StringUtils.array2String(st));
			parm.put("update_by", user.getNickname()+"*"+user.getUser_id());
			boolean b = yjxFormMgrService.delFormMgr(parm);
			if (b){
				this.writeJSON("ok"); 
			} else {
				this.writeJSON("no"); 
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	/**
	 *  分页获取小程序文案列表
	 * @return
	 */
	private String findValue;
	private String findInfoId;
	private String findType;
	public String pageGetCdInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findValue)) {
				param.put("value", findValue);
			}
			if (StringUtils.isNotEmpty(findInfoId)) {
				param.put("info_id", findInfoId);
			}
			if (StringUtils.isNotEmpty(findType)) {
				param.put("type", findType);
			}
			Page page = yjxFormMgrService.pageGetCdInfo(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}
	
	private CdInfoDto cdInfoDto;
	private Object jsonRet;
	public String addCdInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null && cdInfoDto!=null){
    		try {
    			cdInfoDto.setUpdate_by("add:"+user.getUser_id());
    			yjxFormMgrService.addCdInfo(cdInfoDto);
    			jsonRet = renderSucc("新建成功");
			} catch (Exception e) {
				jsonRet = renderError(e.getMessage());
			}
    	} else {
    		jsonRet = renderError("参数错误");
    	}
    	return SUCCESS;
	}
	
	public String updateCdInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null && cdInfoDto!=null){
			try {
				cdInfoDto.setUpdate_by("upd:"+user.getUser_id());
				yjxFormMgrService.updateCdInfo(cdInfoDto);
				jsonRet = renderSucc("");
			} catch (Exception e) {
				jsonRet = renderError(e.getMessage());
			}
		} else {
			jsonRet = renderError("参数错误");
		}
		return SUCCESS;
	}
	public String delCdInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if (user!=null && user.getUser_id()!=null&&ids!=null){
			try {
				Map<String, Object> parm = new HashMap<String, Object>();
				parm.put("ids", ids);
				try {
					parm.put("update_by", user.getUser_id());
					yjxFormMgrService.delCdInfo(parm);
					jsonRet = renderSucc("删除成功");
				} catch (Exception e) {
					jsonRet = renderError("删除失败");
				}
			} catch (Exception e) {
				jsonRet = renderError(e.getMessage());
			}
		} else {
			jsonRet = renderError("参数错误");
		}
		return SUCCESS;
	}
	
	// 获取单个
	private List<CdInfoDto> lisCdinfo;
	public String getOneCdInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String infoId = request.getParameter("id");
		if (user!=null && user.getUser_id()!=null&&infoId!=null){
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("info_id", infoId);
			lisCdinfo = yjxFormMgrService.getOneCdInfo(parm);
		} 
		return SUCCESS;
	}
	
	public String getFindValue() {
		return findValue;
	}
	public void setFindValue(String findValue) {
		this.findValue = findValue;
	}
	public String getFindInfoId() {
		return findInfoId;
	}
	public void setFindInfoId(String findInfoId) {
		this.findInfoId = findInfoId;
	}
	public Object getJsonRet() {
		return jsonRet;
	}
	public void setJsonRet(Object jsonRet) {
		this.jsonRet = jsonRet;
	}
	public YjxFormMgrService getYjxFormMgrService() {
		return yjxFormMgrService;
	}
	public void setYjxFormMgrService(YjxFormMgrService yjxFormMgrService) {
		this.yjxFormMgrService = yjxFormMgrService;
	}
	public Map<String, Object> getResMap() {
		return resMap;
	}
	public void setResMap(Map<String, Object> resMap) {
		this.resMap = resMap;
	}
	public List<YjxFormMgrDto> getFormLis() {
		return formLis;
	}
	public void setFormLis(List<YjxFormMgrDto> formLis) {
		this.formLis = formLis;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getFindFormType() {
		return findFormType;
	}
	public void setFindFormType(String findFormType) {
		this.findFormType = findFormType;
	}
	public CdInfoDto getCdInfoDto() {
		return cdInfoDto;
	}
	public void setCdInfoDto(CdInfoDto cdInfoDto) {
		this.cdInfoDto = cdInfoDto;
	}
	public List<CdInfoDto> getLisCdinfo() {
		return lisCdinfo;
	}
	public void setLisCdinfo(List<CdInfoDto> lisCdinfo) {
		this.lisCdinfo = lisCdinfo;
	}
	public String getFindType() {
		return findType;
	}
	public void setFindType(String findType) {
		this.findType = findType;
	}
}
